﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using WestJourney.Misc;

namespace WestJourney
{
    public partial class UserDisplay : UserControl
    {
        public UserDisplay()
        {
            //CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            this.LoadBackGroundWorker();
        }

        public event EventHandler<UserFinishedEventArgs> MoveFinished;
        BackgroundWorker bgWorker = new BackgroundWorker();

        private void LoadBackGroundWorker()
        {
            this.bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            this.bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
        }

        void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (this.progressBar.Value >= this.progressBar.Maximum && this.MoveFinished != null)
            {
                UserFinishedEventArgs args = new UserFinishedEventArgs();
                args.UserName = this.UserName;
                MoveFinished(this, args);
            }
        }

        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            int stepCount = Convert.ToInt32(e.Argument);
            this.progressBar.Increment(stepCount);
        }

        public string UserName
        {
            get { return this.labelUserName.Text; }
            set { this.labelUserName.Text = value; }
        }

        public void MoveStep(int stepCount)
        {
            this.progressBar.Increment(stepCount);
            if (this.progressBar.Value >= this.progressBar.Maximum && this.MoveFinished!=null)
            {
                UserFinishedEventArgs args = new UserFinishedEventArgs();
                args.UserName = this.UserName;
                MoveFinished(this, args);
            }
        }

        public void MoveStepAsyn(int stepCount)
        {
            bgWorker.RunWorkerAsync(stepCount);
        }
    }
}
